package com.yang.controller;

import com.yang.dao.entity.OnlineTestScore;
import com.yang.dao.entity.OnlineTestTopic;
import com.yang.dao.entity.Student;
import com.yang.service.OnlineTestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by ygs on 2017/7/7.
 */
@Controller
public class TestController {

    @Autowired
    OnlineTestService onlineTestService;


    @RequestMapping("/index")
    public String index(){

        return  "page/login";
    }

    @RequestMapping("/login")
    public  String  login( Student student,HttpServletRequest request){
        if(student.getId()==null || StringUtils.isEmpty(student.getName())){
            request.setAttribute("info","学号或者姓名不能为空！");
            return  "page/login";
        }

        Integer login = onlineTestService.login(student);
        if(login==1){
            return  "page/topic";
        }else{
            request.setAttribute("info","未查询到相关信息");
            return  "page/login";
        }

    }


    @RequestMapping("/exam")
    public  String  examIndex(HttpServletRequest request){
        OnlineTestScore onlineTestScore = onlineTestService.selectScore();
        if(onlineTestScore!=null){
            request.setAttribute("info","你已经完成考试！无法再次开始");
            return  "page/topic";
        }
            return  "page/index";
    }


    @RequestMapping("/score")
    public  String  score(HttpServletRequest request){
        OnlineTestScore onlineTestScore = onlineTestService.selectScore();
        if(onlineTestScore==null){
            request.setAttribute("info","无成绩！请先考试！");
        }else{
            request.setAttribute("info",onlineTestScore.getScore());
        }
        return  "page/score";
    }

    @RequestMapping("/topic")
    public  String  topic(HttpServletRequest request){

        return  "page/topic";
    }

    @RequestMapping("/submitAnswer")
    @ResponseBody
    public  String  submitAnswer(@RequestBody List<OnlineTestTopic> scoreList){
        Integer integer = onlineTestService.calculatScore(scoreList);
        return  "topic.do";
    }

}
